package com.binguo.model.mapper;

import com.binguo.model.core.Mapper;
import com.binguo.model.model.Customer;
import com.binguo.model.wxapi.form.ApiGetCustomerInfoListForm;
import com.binguo.model.wxapi.vo.ApiCustomerVO;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

public interface CustomerMapper extends Mapper<Customer> {

    @Select("SELECT customer.* FROM `customer` \n" +
            "INNER JOIN customer_extend\n" +
            "on customer.id = customer_extend.customer_id\n" +
            "where customer_extend.shop_id = #{shopId}\n" +
            "and customer.phone = #{phone} ")
    Customer getCustomerByShopIdAndPhone(@Param("shopId") Integer shopId,@Param("phone")String phone);

    @Select("select * from customer where phone = #{phone}")
    Customer getCustomerByPhone(@Param("phone")String phone);

    @Select("<script>" +
            "select customer.id as customerId,customer.`name` as customerName,customer.phone as phone,\n" +
            "pay_card.cardNo as cardNo,(pay_card.accountBalance+pay_card.bz_accountBalance+pay_card.wx_accountBalance+pay_card.ykt_accountBalance) AS balance from customer inner join customer_extend  \n" +
            "on customer_extend.customer_id = customer.id \n" +
            "inner join pay_card on pay_card.customerId = customer.id \n" +
            "where customer_extend.shop_id = #{id}\n"+
            "<if test='customer.username != null'>"+
            "and customer.name = #{customer.userName}"+
            "</if>"+
            "<if test='customer.cardNo != null'>"+
            "and pay_card.cardNo = #{customer.cardNo}"+
            "</if>"+
            "<if test='customer.phone != null'>"+
            "and customer.phone = #{customer.phone}"+
            "</if>"+
            "<if test='customer.groupName != null'>"+
            "and customer_extend.group_name = #{customer.groupName}"+
            "</if>"+
            "<if test='customer.startTime != null'>"+
            "<![CDATA[ and customer.created > #{customer.startTime} ]]>"+
            "</if>"+
            "and pay_card.shopId = #{id} " +
            "limit #{customer.page},20"
            +"</script>")
    List<ApiCustomerVO> selectCustomer(@Param("id") Integer id,@Param("customer") ApiGetCustomerInfoListForm getCustomerInfoForm);

    @Select("<script>" +
            "select count(*) from customer inner join customer_extend  \n" +
            "on customer_extend.customer_id = customer.id \n" +
            "inner join pay_card on pay_card.customerId = customer.id \n" +
            "where customer_extend.shop_id = #{id}\n"+
            "<if test='customer.username != null'>"+
            "and customer.name = #{customer.userName}"+
            "</if>"+
            "<if test='customer.cardNo != null'>"+
            "and pay_card.cardNo = #{customer.cardNo}"+
            "</if>"+
            "<if test='customer.phone != null'>"+
            "and customer.phone = #{customer.phone}"+
            "</if>"+
            "<if test='customer.groupName != null'>"+
            "and customer_extend.group_name = #{customer.groupName}"+
            "</if>"+
            "<if test='customer.startTime != null'>"+
            "<![CDATA[ and customer.created > #{customer.startTime} ]]>"+
            "</if>"+
            "and pay_card.shopId = #{id} "+
            "</script>")
    int selectCustomerCount(@Param("id") Integer id,@Param("customer") ApiGetCustomerInfoListForm getCustomerInfoForm);

    @Select("<script>" +
            "select * from customer INNER JOIN customer_extend\n" +
            "on customer.id = customer_extend.id\n" +
            "where  customer_extend.shop_id in \n" +
            "<foreach item='item' index='index' collection='customerIds' open='(' separator=',' close=')'> #{item} </foreach>"+
            "</script>")
    List<Customer> selectPushMessageCustomerFree(@Param("customerIds") List<Integer> customerIds);

    @Select("<script>" +
            "select * from customer INNER JOIN customer_extend\n" +
            "on customer.id = customer_extend.id\n" +
            "where customer.is_pay_message = 1 and customer_extend.shop_id in \n" +
            "<foreach item='item' index='index' collection='shopIds' open='(' separator=',' close=')'> #{item} </foreach>"+
            "</script>")
    List<Customer> selectPushMessageCustomerPay(@Param("shopIds") List<Integer> shopIds);

    @Select("SELECT * FROM `customer` INNER JOIN customer_extend on customer.id = customer_extend.customer_id where customer.phone = #{phone}  and customer_extend.shop_id = #{shopId}")
    Customer selectCustomerByPhoneAndShopId(@Param("phone") String phone,@Param("shopId") Integer shopId);

    List<Integer> shopCustomerIdList(@Param("shopId") Integer shopId);

}